package com.pkutech.carcasher.activity;


import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.model.LatLng;
import com.pkutech.carcasher.R;
import com.pkutech.carcasher.bean.BaseResponse;
import com.pkutech.carcasher.network.SoapApi;
import com.pkutech.carcasher.utils.DateUtils;

import java.util.Date;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action0;
import rx.schedulers.Schedulers;

/**
 * Created by cyy on 2016/6/4.
 */
public class BaseActivity extends AppCompatActivity {
    // 定位相关
    LocationClient mLocClient;

    public MyLocationListenner myListener = new MyLocationListenner();
    Date timeIntervalStart = new Date();
    int intervalMinite=2;//每间隔2分钟

    int loginFlag=0;//判断登陆状态
    String userid="";//登出的时候用户id
    private int myflag=0;
    /**
     * 定位SDK监听函数
     */
    public class MyLocationListenner implements BDLocationListener {

        @Override
        public void onReceiveLocation(BDLocation location) {
            String latitude=String.valueOf(location.getLatitude());
            String longitude=String.valueOf(location.getLongitude());
            Date nowDate=new Date();
            if(loginFlag==0 && DateUtils.compareDate(DateUtils.addMinute(timeIntervalStart,intervalMinite),nowDate)<0){
                //调用发送位置接口
//                SoapApi.updateCoordinateInfoById(latitude,longitude);
                updatePostion(latitude,longitude);
                //更新timeIntervalStart
                timeIntervalStart=nowDate;
            }else if(loginFlag==1 && myflag==0){
                updatePostion2("", "", userid);
                //更新timeIntervalStart
                timeIntervalStart=nowDate;
            }
        }

        public void onReceivePoi(BDLocation poiLocation) {
        }
    }
    public void updatePostion( String latitude, String longitude){
        SoapApi.updateCoordinateInfoById(latitude, longitude)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .doOnSubscribe(new Action0() {
                    @Override
                    public void call() {

                    }
                })
                .subscribe(new Subscriber<BaseResponse>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                    }

                    @Override
                    public void onNext(BaseResponse leftParkingResponse) {

                    }
                });
    }
    public void updatePostion2( String latitude, String longitude,String userid){
        SoapApi.updateCoordinateInfoById2(latitude, longitude, userid)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .doOnSubscribe(new Action0() {
                    @Override
                    public void call() {

                    }
                })
                .subscribe(new Subscriber<BaseResponse>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                    }

                    @Override
                    public void onNext(BaseResponse leftParkingResponse) {
                        myflag=1;
                    }
                });
    }


    // 写一个广播的内部类，当收到动作时，结束activity
    private BroadcastReceiver mFinishReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            ((Activity) context).finish();
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // 在当前的activity中注册关闭广播
        IntentFilter filter = new IntentFilter();
        filter.addAction("finish");
        registerReceiver(mFinishReceiver, filter);

        // 定位初始化
        mLocClient = new LocationClient(this);
        mLocClient.registerLocationListener(myListener);
        LocationClientOption option = new LocationClientOption();
        option.setOpenGps(true); // 打开gps
        option.setCoorType("bd09ll"); // 设置坐标类型
        option.setScanSpan(1000);
        mLocClient.setLocOption(option);
        mLocClient.start();
    }
    /**
     * 初始化toolbar
     * @param toolbar
     */
    protected void initToolbar(Toolbar toolbar){
        setSupportActionBar(toolbar);
        toolbar.setNavigationIcon(R.drawable.ic_arrow_back_white_24dp);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(BaseActivity.this, HomeActivity.class);
                startActivity(intent);
                Intent it = new Intent("finish");
                sendBroadcast(it);
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mFinishReceiver);
    }
}
